package com.ice.app.project.${objName}.controller;

import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.iceframework.core.entity.Page;
import com.ice.app.project.${objName}.service.${className}Service;
import com.ice.app.project.entity.${className};
import com.iceframework.core.base.BaseController;
import com.iceframework.core.entity.Result;

/**
 * 
 * 控制层： ${comments}
 * 
 * @author ${author}
 * @email ${email}
 * @date ${datetime}
 */
@RestController
@RequestMapping("/${objName}")
public class ${className}Controller extends BaseController {

	@Autowired
	private ${className}Service ${objName}Service;

	/**
	 * 数据获取
	 * 
	 * @param request
	 * @return
	 */
	@RequestMapping(value = "/get", method = RequestMethod.GET)
	public Result get${className}(@RequestParam(value = "key") String key, HttpServletRequest request) {
		${className} ${objName} = ${objName}Service.get${className}(key);
		return Result.success().add("${objName}", ${objName});
	}

	/**
	 * 首页加载数据
	 * 
	 * @param pn
	 * @param ${objName}
	 * @param request
	 * @return
	 */
	@RequestMapping(value = "/index/data", method = RequestMethod.POST)
	@ResponseBody
	public Result ${objName}IndexData(${className} ${objName}, Page<${className}> page, HttpServletRequest request) {
		PageHelper.startPage(page.getPageNum(), page.getPageSize(), page.getOrderBy());
		List<${className}> ${objName}s = ${objName}Service.get${className}s(${objName});
		PageInfo<${className}> data = new PageInfo<${className}>(${objName}s, page.getPageSize());
		return Result.success().add("data", data);
	}

	/**
	 * 保存实体
	 * 
	 * @param ${objName}
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value = "/save", method = RequestMethod.POST)
	@ResponseBody
	public Result save${className}(${className} ${objName}) throws Exception {
		if (${objName}Service.save${className}(${objName})) {
			return Result.saveSuccess();
		} else {
			return Result.saveFail();
		}
	}

	/**
	 * 删除实体
	 * 
	 * @param keys
	 * @return
	 */
	@RequestMapping(value = "/del", method = RequestMethod.POST)
	@ResponseBody
	public Result del${className}(String keys) {
		if (${objName}Service.delete${className}s(keys)) {
			return Result.delSuccess();
		} else {
			return Result.delFail();
		}
	}

}